home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech's Sprocket™ / Sprocket-10.14.95 / SprocketStarter / PictureWindow.cp < prev    next >
Encoding:
Text File  |  1995-10-08  |  7.1 KB  |  345 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        PictureWindow.cp
  3.  
  4.     Contains:    A window designed to hold a PICT, no scroll bars...
  5.     Written by: Dave Mark
  6.     Copyright:    ©1995 by Dave Mark, all rights reserved.
  7.  */
  8.  
  9.  
  10. const short            kPictureWindowTemplateID = 1028;
  11. const short            kDefaultPICTResID = 1025;
  12.  
  13.  
  14. #include "PictureWindow.h"
  15. #include <ToolUtils.h>
  16.  
  17.  
  18. MenuHandle            TPictureWindow::fgMenu;
  19. unsigned long        TPictureWindow::fgWindowTitleCount = 0;
  20.  
  21. extern TMenuBar*    gMenuBar;
  22.  
  23. //    methods
  24.  
  25. TPictureWindow::TPictureWindow()
  26. {
  27.     fDraggedPicHandle = nil;
  28.  
  29.     TPictureWindow::fgWindowTitleCount++;    //    Starts out as zero but we want “Picture Window 1”
  30.     this->CreateWindow();    // kNormalWindow is default. Method inherited from TWindow
  31.                             // TWindow::CreateWindow() calls our MakeNewWindow().
  32. }
  33.  
  34.  
  35. TPictureWindow::~TPictureWindow()
  36. {
  37. //    No particular cleanup in this version, since our real purpose was
  38. //    to demonstrate the Drag Manager, not to build a picture handling app.
  39. }
  40.  
  41.     
  42. WindowRef
  43. TPictureWindow::MakeNewWindow( WindowRef behindWindow )
  44. {
  45.     WindowRef    aWindow;
  46.     Str255        titleString;
  47.     GrafPtr        savedPort;
  48.     
  49.     GetPort(&savedPort);
  50.     
  51.     aWindow = GetNewColorOrBlackAndWhiteWindow( kPictureWindowTemplateID,
  52.                             nil, behindWindow );
  53.     
  54.     if (aWindow)
  55.     {
  56.         GetWTitle(aWindow,titleString);
  57.         if (StrLength(titleString) != 0)
  58.         {
  59.             Str255 numberString;
  60.             
  61.             NumToString( fgWindowTitleCount, numberString );
  62.             BlockMove(&numberString[1],&titleString[titleString[0]+1],numberString[0]);
  63.             titleString[0] += numberString[0];
  64.         }
  65.         SetWTitle(aWindow,titleString);
  66.  
  67.         SetPortWindowPort(aWindow);
  68.  
  69.         ShowWindow(aWindow);
  70.     }
  71.     SetPort(savedPort);
  72.  
  73.     return aWindow;
  74. }
  75.  
  76.  
  77. void
  78. TPictureWindow::Draw(void)
  79. {
  80.     PicHandle    pic;
  81.     Rect        r;
  82.     
  83.     r = GetWindowPort(fWindow)->portRect;
  84.     EraseRect( &r );
  85.     
  86.     if ( fDraggedPicHandle == nil )
  87.         pic = this->LoadDefaultPicture();
  88.     else
  89.         pic = fDraggedPicHandle;
  90.  
  91.     this->CenterPict( pic, &r );
  92.     DrawPicture( pic, &r );
  93. }
  94.  
  95.  
  96. void
  97. TPictureWindow::Activate( Boolean activating )
  98. {
  99.     if ( activating )
  100.     {
  101.         InsertMenu( fgMenu, 0 );
  102.         gMenuBar->Invalidate();
  103.     }
  104.     else
  105.         DeleteMenu( mPicture );
  106. }
  107.  
  108.  
  109. void
  110. TPictureWindow::Click( EventRecord * )
  111. {
  112.     this->Select();
  113. }
  114.  
  115.  
  116. void
  117. TPictureWindow::ClickAndDrag( EventRecord *eventPtr )
  118. {
  119.     OSErr            err;
  120.     DragReference   dragRef;
  121.     RgnHandle       dragRegion, tempRgn;
  122.     Rect            itemBounds;
  123.     Handle            flavorDataHandle;
  124.     
  125.     err = NewDrag( &dragRef );
  126.     if ( err != noErr )
  127.         return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  128.  
  129.     if ( fDraggedPicHandle == nil )
  130.         flavorDataHandle = (Handle)this->LoadDefaultPicture();
  131.     else
  132.         flavorDataHandle = (Handle)fDraggedPicHandle;
  133.  
  134.     HLock( flavorDataHandle );
  135.  
  136.     err = AddDragItemFlavor( dragRef,
  137.                             (ItemReference)fWindow,// no particular reason, could use any number here
  138.                             (FlavorType) 'PICT',
  139.                             (Ptr)*flavorDataHandle,
  140.                             GetHandleSize( (Handle)flavorDataHandle ),
  141.                             (FlavorFlags)0 );
  142.  
  143.     HUnlock( flavorDataHandle );
  144.  
  145.     if ( err != noErr )
  146.     {
  147.         DisposeDrag( dragRef );
  148.         return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  149.     }
  150.     
  151.     itemBounds = this->GetContentsBounds();
  152.     
  153.     err = SetDragItemBounds( dragRef, (ItemReference)fWindow, &itemBounds );
  154.     if ( err != noErr )
  155.     {
  156.         DisposeDrag( dragRef );
  157.         return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  158.     }
  159.     
  160.     dragRegion = NewRgn();
  161.     RectRgn( dragRegion, &itemBounds );
  162.     tempRgn = NewRgn();
  163.     CopyRgn( dragRegion, tempRgn );
  164.     InsetRgn( tempRgn, 1, 1 );
  165.     DiffRgn( dragRegion, tempRgn, dragRegion );
  166.     DisposeRgn( tempRgn );
  167.     
  168.     err = TrackDrag( dragRef, eventPtr, dragRegion );
  169.     DisposeRgn( dragRegion );
  170.     DisposeDrag( dragRef );
  171.     return;    // Should return err, but needs a change to TWindow::ClickAndDrag return type
  172. }
  173.  
  174.  
  175. OSErr
  176. TPictureWindow::DragEnterWindow( DragReference dragRef )
  177. {
  178.     fCanAcceptDrag = IsPictFlavorAvailable( dragRef );
  179.     fIsWindowHighlighted = false;
  180.     
  181.     if ( fCanAcceptDrag )
  182.         return noErr;
  183.     else
  184.         return dragNotAcceptedErr;
  185. }
  186.  
  187.  
  188. OSErr
  189. TPictureWindow::DragInWindow( DragReference dragRef )
  190. {
  191.     DragAttributes    attributes;
  192.     RgnHandle        tempRgn;
  193.  
  194.     GetDragAttributes( dragRef, &attributes );
  195.     
  196.     if ( (! fCanAcceptDrag) || (! (attributes & dragHasLeftSenderWindow)) 
  197.         || (attributes & dragInsideSenderWindow) )
  198.         return dragNotAcceptedErr;
  199.     
  200.     if ( this->IsMouseInContentRgn( dragRef ) )
  201.     {
  202.         if ( ! fIsWindowHighlighted )
  203.         {
  204.             tempRgn = NewRgn();
  205.             RectRgn( tempRgn, &GetWindowPort(fWindow)->portRect );
  206.             
  207.             if ( ShowDragHilite( dragRef, tempRgn, true ) == noErr )
  208.                 fIsWindowHighlighted = true;
  209.                 
  210.             DisposeRgn(tempRgn);
  211.         }
  212.     }
  213.         
  214.     return noErr;
  215. }
  216.  
  217.  
  218. OSErr
  219. TPictureWindow::DragLeaveWindow( DragReference dragRef )
  220. {
  221.     if ( fIsWindowHighlighted )
  222.         HideDragHilite( dragRef );
  223.     
  224.     fIsWindowHighlighted = false;
  225.     fCanAcceptDrag = false;
  226.     
  227.     return noErr;
  228. }
  229.  
  230.  
  231. OSErr
  232. TPictureWindow::HandleDrop( DragReference dragRef )
  233. {
  234.     OSErr            err;
  235.     Size            dataSize;
  236.     ItemReference    item;
  237.     FlavorFlags        flags;
  238.     DragAttributes    attributes;
  239.  
  240.     GetDragAttributes( dragRef, &attributes );
  241.     
  242.     if ( attributes & dragInsideSenderWindow )
  243.         return dragNotAcceptedErr;
  244.  
  245.     err = GetDragItemReferenceNumber( dragRef, 1, &item );
  246.     if ( err == noErr )
  247.         err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
  248.  
  249.     if ( err == noErr )
  250.     {
  251.         err = GetFlavorDataSize( dragRef, item, 'PICT', &dataSize);
  252.         if  (err == noErr )
  253.         {
  254.             fDraggedPicHandle = (PicHandle)TempNewHandle( dataSize, &err );
  255.             
  256.             if ( fDraggedPicHandle == nil )
  257.                 fDraggedPicHandle = (PicHandle)NewHandle( dataSize );
  258.  
  259.             if ( fDraggedPicHandle == nil )
  260.                 err = dragNotAcceptedErr;
  261.             else
  262.             {
  263.                 HLock( (Handle)fDraggedPicHandle );
  264.                 err = GetFlavorData( dragRef, item, 'PICT',
  265.                         *fDraggedPicHandle, &dataSize, 0L );
  266.                 HUnlock( (Handle)fDraggedPicHandle );
  267.  
  268.                 if ( err != noErr)
  269.                 {
  270.                     err = dragNotAcceptedErr;
  271.                     DisposeHandle( (Handle)fDraggedPicHandle );
  272.                     fDraggedPicHandle = nil;
  273.                 }
  274.                 else
  275.                 {
  276.                     SetPortWindowPort( fWindow );
  277.                     InvalRect( &(GetWindowPort(fWindow)->portRect) );
  278.                 }
  279.             }
  280.         }
  281.     }
  282.     
  283.     return( err );
  284. }
  285.  
  286.  
  287. PicHandle
  288. TPictureWindow::LoadDefaultPicture()
  289. {
  290.     PicHandle    pic;
  291.     
  292.     pic = GetPicture( kDefaultPICTResID );
  293.     
  294.     if ( pic == nil )
  295.     {
  296.         DebugStr( (StringPtr) "\pCould not load PICT resource!" );
  297.         return (PicHandle)nil;
  298.     }
  299.     else
  300.         return( pic );
  301. }
  302.  
  303.  
  304. void
  305. TPictureWindow::CenterPict( PicHandle picture, Rect *destRectPtr )
  306. {
  307.     Rect    windRect, pictRect;
  308.     
  309.     windRect = *destRectPtr;
  310.     pictRect = (**( picture )).picFrame;
  311.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  312.                            windRect.top     - pictRect.top);
  313.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  314.                           (windRect.bottom - pictRect.bottom)/2);
  315.     *destRectPtr = pictRect;
  316. }
  317.  
  318.  
  319. Boolean
  320. TPictureWindow::IsPictFlavorAvailable( DragReference dragRef )
  321. {
  322.     unsigned short    numItems;
  323.     FlavorFlags        flags;
  324.     OSErr            err;
  325.     ItemReference    item;
  326.     
  327.     CountDragItems( dragRef, &numItems );
  328.     
  329.     if ( numItems < 1 )
  330.         return( false );
  331.     
  332.     err = GetDragItemReferenceNumber( dragRef, 1, &item );
  333.     if ( err == noErr )
  334.         err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
  335.     
  336.     return( err == noErr );
  337. }
  338.  
  339.  
  340. void
  341. TPictureWindow::SetUpStaticMenu( void )
  342. {
  343.     TPictureWindow::fgMenu = gMenuBar->GetMenuFromCMNU( mPicture );
  344. }
  345.